package com.good.child.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.good.child.R;
import com.good.child.bean.GenerateBean;
import com.good.child.iview.EmptyIView;
import com.good.child.pressent.EmptyPresenter;
import com.good.child.ui.utils.AIHelpUtil;
import com.yimidida.library.base.BaseActivity;
import com.yimidida.library.base.LoadingDialog;
import com.yimidida.library.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create on: 2024/8/25
 * by Mr.Zhu
 * Description: TODO
 */
public class TestActivity extends BaseActivity<EmptyIView, EmptyPresenter> {
    @BindView(R.id.btn)
    Button btn;
    @BindView(R.id.btn1)
    Button btn1;
    @BindView(R.id.btn2)
    Button btn2;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.image)
    ImageView imageView;
    private LoadingDialog loadingDialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected EmptyPresenter initPresenter() {
        return new EmptyPresenter(this);
    }

    @Override
    protected void initView() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        testDate();
                    }
                }).start();
            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        sse();
                    }
                }).start();
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        sseWithParams();
                    }
                }).start();
            }
        });
    }

    private void testDate(){
        StringBuffer buffer = new StringBuffer();
        AIHelpUtil.getInstance().requestText2Text("去上海旅行，应该避开哪些地方", new AIHelpUtil.Text2TextCallBack() {
            @Override
            public void onDataBack(GenerateBean bean) {
                buffer.append(bean.getContent());
                tv.setText(buffer.toString());
            }
        });
    }

    void sse(){
        setShowLoading();
        AIHelpUtil.getInstance().requestText2Image("中国风。去上海旅行，应该避开哪些地方", new AIHelpUtil.Text2TextCallBack() {
            @Override
            public void onDataBack(GenerateBean generateBean) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (generateBean != null) {
                            dismiss();
                            Glide.with(TestActivity.this)
                                    .load(generateBean.getFileUrl())
                                    .into(imageView);
                        }
                    }
                });

            }
        });
    }

    void sseWithParams(){
//        setShowLoading();
//        AIHelpUtil.getInstance().requestImage2Image(AIHelpUtil.AIDefine.CAMERA_KA_TONG, "",new AIHelpUtil.Text2TextCallBack() {
//            @Override
//            public void onDataBack(GenerateBean generateBean) {
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        dismiss();
//                        if (generateBean != null) {
//                            Glide.with(TestActivity.this)
//                                    .load(generateBean.getFileUrl())
//                                    .into(imageView);
//                        }
//                    }
//                });
//            }
//        });
    }

    public void setShowLoading() {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);
        }
        loadingDialog.showLoading();
    }


    private void dismiss() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }
}
